package com.boarsoft.boar.batch.demo.flow;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import com.boarsoft.boar.batch.demo.entity.Demo1;
import com.boarsoft.flow.core.bean.ProcessHandler;
import com.boarsoft.hibernate.biz.SimpleBizImpl;
import com.boarsoft.hibernate.dao.SimpleDao;

public class DemoFlowBatchProcess4 extends SimpleBizImpl implements ProcessHandler {
	@Autowired
	protected SimpleDao dao;

	// protected Query query;

	@PostConstruct
	public void init() {
		// query = dao.createSQLQuery("insert demo1_1 (iId, vF1, vF2) values (?,
		// ?, ?)");
	}

	/**
	 * 插入demo1_1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		Map<String, Object> pm = (Map<String, Object>) data;
		List<Demo1> lt = (List<Demo1>) pm.get("list");
		Session ss = dao.getASession();
		Query query = ss.createSQLQuery("insert demo1_1 (iId, vF1, vF2) values (?, ?, ?)");
		for (Demo1 o : lt) {
			query.setLong(0, o.getId());
			query.setString(1, o.getF1());
			query.setString(2, o.getF2());
			query.executeUpdate();
		}
		ss.flush();
		ss.clear();
		return data;
	}
}
